var appInstance = getApp()
var util = require('../../utils/util.js');
Page({
  data: {
    in: '1000',
    out: '2000',
    rest: '1000',
    index: 0,
    //picker组件数据
    multiArray: [['2017', '2018'], ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']],
    objectMultiArray: [
      [
        {
          id: 0,
          name: '2017'
        },
        {
          id: 1,
          name: '2018'
        }
      ], [
        {
          id: 0,
          name: '01'
        },
        {
          id: 1,
          name: '02'
        },
        {
          id: 2,
          name: '03'
        },
        {
          id: 3,
          name: '04'
        },
        {
          id: 4,
          name: '05'
        },
        {
          id: 5,
          name: '06'
        },
        {
          id: 6,
          name: '07'
        },
        {
          id: 7,
          name: '08'
        },
        {
          id: 8,
          name: '09'
        },
        {
          id: 9,
          name: '10'
        },
        {
          id: 10,
          name: '11'
        },
        {
          id: 11,
          name: '12'
        }
      ]
    ],
   
    date: '2016-09-01',
    time: '12:01',
    year: 2000,
    month: 11,
    //明细条目存放数组
    listData: [
    ],
    multiIndex: appInstance.data.multiIndex
  },
  onLoad: function () {
    //util获取当前时间，截取年月信息
    var time = util.formatTime(new Date());
    var dotp = time.indexOf("/");
    var year = time.substr(0, 4);
    var month = time.substr(dotp + 1, 2);
    this.time = time;
    this.year = parseInt(year);
    this.month = parseInt(month);
  },

  onShow: function () {
    //数据库返回收入支出
    wx.request({
      url: 'https://hi.slickghost.com/WebTest/Operate',
      data: {
        choice: 'findin_out',
        openid: appInstance.globalData.openid,
        year: this.year,
        month: this.month
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: (res) => {
        var in_out = JSON.stringify(res.data);
        var str = in_out.substr(1, in_out.length - 2);
        var dotp1 = in_out.indexOf(",");
        var dotp2 = in_out.indexOf(",", dotp1 + 1);
        this.setData({
          out: str.substr(0, dotp1 - 1),
          in: str.substr(dotp1, dotp2 - dotp1 - 1),
          rest: str.substr(dotp2, str.length - dotp2),
        })
      }
    }),
    //数据库返回账单明细
      wx.request({
        url: 'https://hi.slickghost.com/WebTest/Operate',
        data: {
          choice: 'findaccount',
          openid: appInstance.globalData.openid,
          year: this.year,
          month: this.month
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        //动态赋值listData
        success: (list) => {
          var preDay = "00";
          
          if (list.data.length==0){
            var that = this;
            that.setData({
              listData: []
            })
          }
          for (var i = 0; i < list.data.length; i++) {
            var day = list.data[i].currenttime.slice(8, 10);
            var style = list.data[i].tname;
            var money = list.data[i].money;
            var tip = list.data[i].tip;
            //重复的日期只显示最前面的那条
            if (day == preDay) {
              preDay = day;
              day = "";
              var that = this,
                array = that.data.listData;
              array[i] = { "code": day, "text": style, "type": money ,"ex" : tip };
              that.setData({
                listData: array
              })
            }
            else {
              var that = this,
                array = that.data.listData;
              array[i] = { "code": day, "text": style, "type": money, "ex" : tip};
              that.setData({
                listData: array
              })
              preDay = day;
            }
          }
        }
      })
  },
  //picker选择器数组的改变
  bindMultiPickerChange: function (e) {
    this.setData({
      multiIndex: e.detail.value,
    })
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    this.year = parseInt(data.multiArray[0][data.multiIndex[0]]);
    this.month = parseInt(data.multiArray[1][data.multiIndex[1]]);
    this.onShow();
  },
  //picker选择器行的改变
  bindMultiPickerColumnChange: function (e) {
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        switch (data.multiIndex[0]) {
          case 0:
            data.multiArray[1] = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
            break;
          case 1:
            data.multiArray[1] = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
            break;
        }
        data.multiIndex[1] = 0;
        break;
    }
    this.setData(data);
  }
})
